package com.jacky.ws.config;

import com.jacky.ws.handler.BusinessWebSocketHandler;
import com.jacky.ws.interceptors.CustomerHandshakeInterceptor;
import jakarta.websocket.HandshakeResponse;
import jakarta.websocket.server.HandshakeRequest;
import jakarta.websocket.server.ServerEndpointConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

import java.util.Map;

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    private final Logger LOGGER = LoggerFactory.getLogger(WebSocketConfig.class);
    private final BusinessWebSocketHandler webSocketHandler;
    private final CustomerHandshakeInterceptor interceptor;

    public WebSocketConfig(BusinessWebSocketHandler webSocketHandler, CustomerHandshakeInterceptor interceptor) {
        this.webSocketHandler = webSocketHandler;
        this.interceptor = interceptor;
    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(webSocketHandler,"/business")
                .addInterceptors(interceptor)
                .setAllowedOrigins("*");
    }
}
